package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.common.query.DestinationQuery;
import cn.wolfcode.luowowo.common.query.TravelQuery;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/destination")
public class DestinationController {

    @Reference
    private IRegionService regionService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyCatalogService strategyCatalogService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private ITravelService travelService;

    @RequestMapping("")
    public String index(Model model){
        //热门区域
        List<Region> regions = regionService.listHotRegions();
        model.addAttribute("hotRegions", regions);
        return "destination/index";
    }

    @RequestMapping("/listHotDestByRegionId")
    public String listHotDestByRegionId(@ModelAttribute("regionId") Long regionId, Model model) {
        //regionId
        List<Destination> list = destinationService.listHotDestByRegionId(regionId);

        //目的地分栏
        List<Destination> leftDests = list.subList(0, list.size() / 2);
        List<Destination> rightDests = list.subList(list.size() / 2, list.size());

        model.addAttribute("leftDests",leftDests);
        model.addAttribute("rightDests",rightDests);
        return "destination/hotdestTpl";
    }

    @RequestMapping("/guide")//游玩指南
    public String guide(Long id, Model model) {
        List<Destination> toasts = destinationService.listToasts(id);
        model.addAttribute("toasts",toasts);

        //目的地
        model.addAttribute("dest", toasts.remove(toasts.size() - 1));
        //该目的地下的分类
        model.addAttribute("catalogs", strategyCatalogService.listByDestId(id));
        //点击量前3的文章
        model.addAttribute("strategyDetails",strategyDetailService.listViewnumTop3(id));
        return "destination/guide";
    }

    @RequestMapping("/surveyPage")//点击概况下栏中的分类,跳转到的页面
    public String surveyPage(@ModelAttribute("qo") DestinationQuery qo, Model model) {

        //吐司
        List<Destination> toasts = destinationService.listToasts(qo.getDestId());
        model.addAttribute("toasts",toasts);

        //目的地
        model.addAttribute("dest",toasts.remove(toasts.size()-1));

        return "destination/survey";
    }

    @RequestMapping("/survey")//切换分类发送的异步请求
    public String survey(@ModelAttribute("qo") DestinationQuery qo, Model model) {

        List<StrategyCatalog> catalogs = strategyCatalogService.listByDestId(qo.getDestId());
        //catalogs
        model.addAttribute("catalogs", catalogs);//该目的地下的所有分类
        for (StrategyCatalog catalog : catalogs) {

            if (catalog.getId().equals(qo.getCatalogId())) {//如果被选中的
                model.addAttribute("catalog", catalog);//就回显此分类

                //展示分类中的第一篇文章
                StrategyDetail detail = catalog.getDetails().get(0);
                StrategyContent content = strategyDetailService.getContent(detail.getId());
                detail.setStrategyContent(content);//文章内容
                model.addAttribute("detail", detail);//只展示第一篇文章
            }
        }
        return "destination/surveyTpl";
    }

    @RequestMapping("/travels")
    public String travels(@ModelAttribute("qo") TravelQuery qo, Model model) {
        //pageInfo
        model.addAttribute("pageInfo", travelService.query(qo));
        return "destination/travelTpl";
    }
}
